package com.meishij.customerview;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.meishij.R;

/**
 * Created by Administrator on 2016/10/15.
 * 我是youngKaka 我的心愿是世界和平
 */

public class GuoLoadDialog extends AlertDialog {

    private View loadingView;
    private ImageView iv_yue1;
    private ImageView iv_yong;
    private ImageView iv_yue2;
    private ImageView iv_dong;
    private ImageView iv_ni;
    private int dialogwidth;

    private View[] dialogview = new View[5];
    private float viewHeight;

    public GuoLoadDialog(Context context) {
        super(context);
    }

    public GuoLoadDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected GuoLoadDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        loadingView = LayoutInflater.from(getContext()).inflate(R.layout.item_shihua_dialog, null);
        setContentView(loadingView);
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        dialogwidth = (int) (120 * dm.density);
        viewHeight = 25 * dm.density;
        lp.width = dialogwidth;
        lp.height = dialogwidth;
        dialogWindow.setAttributes(lp);

        iv_yue1 = ((ImageView) findViewById(R.id.iv_shihua_guo_yue1));
        dialogview[0] = iv_yue1;
        iv_yong = ((ImageView) findViewById(R.id.iv_shihua_guo_yong));
        dialogview[1] = iv_yong;
        iv_yue2 = ((ImageView) findViewById(R.id.iv_shihua_guo_yue2));
        dialogview[2] = iv_yue2;
        iv_dong = ((ImageView) findViewById(R.id.iv_shihua_guo_dong));
        dialogview[3] = iv_dong;
        iv_ni = ((ImageView) findViewById(R.id.iv_shihua_guo_ni));
        dialogview[4] = iv_ni;
    }


    public void animateGuo(final View itemview, final int pos) {
        //补间动画
        TranslateAnimation animation = new TranslateAnimation(0, 0, 0, (float) (-viewHeight * 2.5));
        animation.setDuration(600);
        //不保持动画的状态
        animation.setFillAfter(false);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                itemview.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                itemview.setVisibility(View.GONE);
                if (pos != 4) {
                    int next = pos + 1;
                    animateGuo(dialogview[next], next);
                } else {
                    animateGuo(dialogview[0], 0);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        itemview.startAnimation(animation);
    }


    @Override
    public void show() {
        super.show();
        //子线程进行动画
        new Thread() {
            @Override
            public void run() {
                animateGuo(dialogview[0], 0);
            }
        }.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        for (int i = 0; i < dialogview.length; i++) {
            dialogview[i].clearAnimation();
            dialogview[i].setVisibility(View.GONE);
        }
    }

}
